<?php

namespace App\Repository\Statist;

use App\Models\Statist\OrderStatistModel as model;
use App\Repository\BaseRepository;

class OrderStatistRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    parent::__construct();
  }
  //列表
  public function index($where)
  {
    $cron = $where;
    $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    $data = $this->search($this->model, $cron);
    return $data;
  }
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'created'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'created' => '生成日期不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $insertData['shop_id'] = !empty($data['shop_id']) ? $data['shop_id'] : (!empty($data->user->shop_id) ? $data->user->shop_id : '');
    $insertData['created_at'] = date('Y-m-d H:i:s'); // 导入时间
    if (empty($insertData['created'])) {
      $insertData['created'] = date('Y-m-d'); // 时间处理时间
    }
    // 判断是否存在 如果存在修改
    $isTrue = $this->model->where(['status' => $insertData['status'], 'created' => $insertData['created'], 'shop_id' => $insertData['shop_id']])->first();
    if ($isTrue) {
      $returns = $this->model->where(['status' => $insertData['status'], 'created' => $insertData['created'], 'shop_id' => $insertData['shop_id']])->update($insertData);
      return $this->returnMsg(200, '设置成功');
    }
    $res = $this->model->insertGetId($insertData);
    if ($res) {
      return $this->returnMsg(200, '设置成功');
    }
    return $this->returnMsg(1, '上传失败');
  }
  public function getAll($where, $field = ['*'], $orderby = 'created', $acs = 'DESC')
  {
    return $this->model->where($where)->select($field)->orderBy($orderby, $acs)->get();
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
}
